package com.yuan.yblog.portal.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class GlobalCorsConfig {
    /**
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //1、允许所有域名进行跨域调用
        config.addAllowedOriginPattern("*");
        //2、放行全部原始头信息
        config.addAllowedHeader("*");
        //3、允许所有请求方法（跨域调用）
        config.addAllowedMethod("*");
        //4、允许发送cookie（跨域）
        config.setAllowCredentials(true);
        //5、添加映射路径，我们拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }
}
